package org.itboys.shop.service;

import com.google.common.collect.Lists;
import org.itboys.member.entity.Member;
import org.itboys.member.service.MemberService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.page.PageQueryParam;
import org.itboys.mongodb.utils.query.PageQueryUtils;
import org.itboys.pay.constant.PayConstants;
import org.itboys.shop.entity.OrderSellRecord;
import org.itboys.shop.entity.ShopSeller;
import org.itboys.trade.constant.OrderConstant;
import org.itboys.trade.entity.Order;
import org.itboys.trade.entity.PrePay;
import org.itboys.trade.entity.TicketOrder;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sun.security.krb5.internal.Ticket;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 订单销售服务类
 * Created by Administrator on 2016/7/23.
 */
@Service
public class OrderSellService extends BaseService<OrderSellRecord, Long> {
	@Autowired
	private MemberService memberService;
	@Autowired
	private ShopSellerService sellerService;
    @Resource(name = "sellerDS")
    private MongoDataSource ds;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return ds;
    }

    @Override
    protected Class<OrderSellRecord> getEntityClass() {
        return OrderSellRecord.class;
    }
    public void saveRecord(Order order,PrePay prePay){
    	OrderSellRecord record=new OrderSellRecord();
    	record.setActualMoney(prePay.getAmount());
    	record.setCenterId(order.getCenterId());
    	record.setOrderNo(order.getOrderNo());
    	Member member=memberService.getById(order.getMemberId());
    	if (member!=null) {
    		record.setPayName(member.getNickname());
		}
    	record.setPayNo(prePay.getThirdTradeNo());
    	record.setPayType(prePay.getPayType());
    	ShopSeller seller=sellerService.getById(order.getSellerId());
    	if (null!=seller) {
    		record.setSellerName(seller.getShopName());
    		record.setPhoneNo(seller.getLinkPhone());
		}
    	record.setTradMoney(order.getTotalMoney());
    	save(record);
    }
    public void saveRecord(TicketOrder order,PrePay prePay){
    	OrderSellRecord record=new OrderSellRecord();
    	record.setActualMoney(prePay.getAmount());
    	record.setCenterId(order.getCenterId());
    	record.setOrderNo(order.getOrderNo());
    	Member member=memberService.getById(order.getMemberId());
    	if (member!=null) {
    		record.setPayName(member.getNickname());
		}
    	record.setPayNo(prePay.getThirdTradeNo());
    	record.setPayType(prePay.getPayType());
    	ShopSeller seller=sellerService.getById(order.getSellerId());
    	if (null!=seller) {
    		record.setSellerName(seller.getShopName());
    		record.setPhoneNo(seller.getLinkPhone());
		}
    	record.setTradMoney(order.getTotalMoney());
    	save(record);
    }

    public Page<OrderSellRecord> queryCenterSellRecord(HttpServletRequest request, long centerId) {
        PageQueryParam page = PageQueryUtils.preparePageQueryParam(request);
        Query<OrderSellRecord> query = getMongoDataSource().createQuery(OrderSellRecord.class)
                .field("centerId").equal(centerId)
                .field("status").in(Lists.newArrayList(OrderConstant.WAIT_RECEIVE,
                        OrderConstant.WAIT_APPRAISE, OrderConstant.FINISH,
                        OrderConstant.BACK, OrderConstant.BACK_OK,
                        OrderConstant.BACK_MONEY, OrderConstant.BACK_MONEY_OK,
                        TicketOrder.status_payed, TicketOrder.status_success,
                        TicketOrder.status_comment))
                .order("-ut");
        final long count = getMongoDataSource().getCount(query);
        /* 返回列表 */
        final List<OrderSellRecord> list = query.order(page.getSortField())
                .offset(page.getPageIndex()).limit(page.getPageSize()).asList();

        return new Page<OrderSellRecord>(list, page.getPageIndex(), page.getPageSize(), count);
    }
}
